package com.hxgz.steel.service.Impl;

import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.NavigationManagerMapper;
import com.hxgz.steel.domain.NavigationManager;
import com.hxgz.steel.dto.DropValueDto;
import com.hxgz.steel.service.NavigationManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NavigationManagerServiceImp implements NavigationManagerService {

  @Autowired
  NavigationManagerMapper navigationManagerMapper;


  @Override
  public List<NavigationManager> getBuyerServiceList() {
    List<NavigationManager> list = navigationManagerMapper.getBuyServiceList();
    return list;
  }

  @Override
  public List<NavigationManager> getSellerServiceList() {
    return navigationManagerMapper.getSellerServiceList();
  }

  @Override
  public List<NavigationManager> getAfterSellerServiceList() {
    return navigationManagerMapper.getAfterSellerList();
  }

  @Override
  public List<NavigationManager> getShopServiceList() {
    return navigationManagerMapper.getShopServiceList();
  }

  @Override
  public List<NavigationManager> getMoneyServiceList() {
    return navigationManagerMapper.getMoneyServiceList();
  }

  @Override
  public List<NavigationManager> getAboutUsList() {
    return navigationManagerMapper.getAboutUsList();
  }

  @Override
  @Transactional
  public ServerResponse update(String id, String title, String content) {
    NavigationManager navigationManager = navigationManagerMapper
        .selectByPrimaryKey(Long.valueOf(id));
    navigationManager.setTitle(title);
    navigationManager.setContent(content);
    int i = navigationManagerMapper.updateByPrimaryKeySelective(navigationManager);
    return i == 1 ? ServerResponse.createBySuccessMessage("修改成功") : ServerResponse.createByError();
  }

  @Override
  @Transactional
  public ServerResponse updateStatus(String id) {
    NavigationManager navigationManager = navigationManagerMapper
        .selectByPrimaryKey(Long.valueOf(id));
    if (navigationManager.getStatus() == 0) {
      navigationManager.setStatus(1);
    } else {
      navigationManager.setStatus(0);
    }
    int i = navigationManagerMapper.updateByPrimaryKeySelective(navigationManager);
    return i == 1 ? ServerResponse.createBySuccessMessage("修改成功") : ServerResponse.createByError();
  }


  @Override
  public ServerResponse checkId(String id) {
    NavigationManager navigationManager = navigationManagerMapper
        .selectByPrimaryKey(Long.valueOf(id));
    return ServerResponse.createBySuccess(navigationManager);
  }

  @Override
  public List<DropValueDto> getDropDownList() {
    List<NavigationManager> list = navigationManagerMapper.getAllList();
    ArrayList<DropValueDto> dropValueList = new ArrayList<>();
    list.forEach(navigationManagerType -> {
      DropValueDto dropValueVo = new DropValueDto();
      dropValueVo.setLabel(navigationManagerType.getTitle());
      dropValueVo.setValue(Integer.valueOf(navigationManagerType.getId().toString()));
      dropValueList.add(dropValueVo);
    });
    return dropValueList;
  }


}
